package com.songtech.cmdrecord.controller;

import com.github.pagehelper.PageInfo;
import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.cmdrecord.service.CmdRecordService;
import com.songtech.login.model.SysPosUser;
import com.songtech.web.entry.SysPosTerminalHistory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:上下行命令信息controller
 *
 * @Author: rentie
 * @Date: 2018/4/12 9:45
 **/

@Controller
@RequestMapping("/cmdrecord")
public class ComRecordController {

    private Logger logger = LoggerFactory.getLogger(ComRecordController.class);

    @Autowired
    private CmdRecordService cmdRecordService;

    /**
     * 查询上下行命令信息
     * @param sender 发送者
     * @param session
     * @param currentPage 当前页
     * @param pageSize    每页数量
     * @return
     */
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult selectCmdRecord(HttpSession session, Integer sender, Integer currentPage , Integer pageSize){
        /**
         * 获取session中到user信息
         */
         SysPosUser user=(SysPosUser)session.getAttribute("loginUser");
          String imei=user.getImei();
        /*String imei="865205032426073";*/
        Map<Object, Object> map = new HashMap<>();
        try {
            PageInfo<SysPosTerminalHistory> list= cmdRecordService.queryComdRecord(imei,sender, currentPage, pageSize);

            /**
             * 查询的列表数据
             */
            map.put("data",list.getList());
            /**
             * 数据总数
             */
            map.put("total",list.getTotal());


        } catch (Exception e) {
            logger.error("查询上下行命令信息异常信息:"+e.getMessage(),e);
            return new JsonResult(map, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(map, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }
}
